package com.tjp.eight.ui.fragment.forgetpass;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.tjp.eight.LoginEvent;
import com.tjp.eight.MainActivity;
import com.tjp.eight.R;
import com.tjp.eight.ResultModel;
import com.tjp.eight.RxBus;
import com.tjp.eight.comment.AppManager;
import com.tjp.eight.constant.ConstantUtils;
import com.tjp.eight.ui.fragment.BaseFragment;
import com.tjp.eight.ui.fragment.setpassword.SetPModel;
import com.tjp.eight.ui.fragment.setpassword.SetPassModel;
import com.tjp.eight.utils.CodeUtils;
import com.tjp.eight.utils.DescriptUtils;
import com.tjp.eight.utils.IsSuccessUtils;
import com.tjp.eight.utils.SharePreferencesUtils;
import com.tjp.eight.utils.StringUtils;
import com.tjp.eight.utils.TimeDown;
import com.tjp.eight.utils.ToatUtils;

/**
 * Created by Administrator on 2017/3/28.
 */
public class FragmentForGetPass extends BaseFragment<FortGetPassPresenter> implements FGpassView {
    private EditText editTextPhone;
    private EditText editTextCode;
    private EditText editTextMsgCode;
    private EditText editTextPass;
    private TextView textViewGetMsgCode;
    private TextView tv_btn_login;
    private ImageView reg_msg_code;
    private TimeDown timeDown;

    private long time = -1;

    @Override
    protected FortGetPassPresenter createPresenter() {
        return new FortGetPassPresenter(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = super.onCreateView(inflater, container, savedInstanceState);
        if (contentView == null) {
            contentView = inflater.inflate(R.layout.fragment_forget_pass, container, false);
        }
        initViews();
        return contentView;
    }

    void initViews() {
        editTextPhone = (EditText) contentView.findViewById(R.id.ed_phone);
        editTextCode = (EditText) contentView.findViewById(R.id.et_code);
        editTextMsgCode = (EditText) contentView.findViewById(R.id.ed_phone_code);
        editTextPass = (EditText) contentView.findViewById(R.id.ed_pass);

        textViewGetMsgCode = (TextView) contentView.findViewById(R.id.tv_get_code);
        tv_btn_login = (TextView) contentView.findViewById(R.id.tv_btn_login);
        reg_msg_code = (ImageView) contentView.findViewById(R.id.reg_msg_code);
        reg_msg_code.setImageBitmap(CodeUtils.getInstance().createBitmap());
        reg_msg_code.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reg_msg_code.setImageBitmap(CodeUtils.getInstance().createBitmap());
            }
        });
        textViewGetMsgCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getMsgCode();
            }
        });
        editTextPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                changeGetMsgCode();
                changeSub();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        editTextCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                changeGetMsgCode();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        editTextMsgCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                changeSub();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        editTextPass.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                changeSub();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        tv_btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                presenter.setPass(getSJson());
            }
        });
    }

    String getSJson() {
        SetPModel setPModel = new SetPModel();
        setPModel.mobile = editTextPhone.getText().toString();
        setPModel.mobilecode = editTextMsgCode.getText().toString();
        setPModel.pwd = editTextPass.getText().toString();
        return new Gson().toJson(setPModel);
    }

    void changeGetMsgCode() {
        if (StringUtils.isMobileNO(editTextPhone.getText().toString())
                && CodeUtils.getInstance().getCode().equals(editTextCode.getText().toString())
                && timeDown==null
                && !StringUtils.isNullOrEmpty(editTextCode.getText().toString())) {
            textViewGetMsgCode.setEnabled(true);
        } else {
            textViewGetMsgCode.setEnabled(false);
        }
    }

    void changeSub() {
        if (StringUtils.isMobileNO(editTextPhone.getText().toString())
                && !StringUtils.isNullOrEmpty(editTextMsgCode.getText().toString())
                /** && editTextMsgCode.getText().toString().length() == 6**/
                && !StringUtils.isNullOrEmpty(editTextPass.getText().toString())
                && editTextPass.getText().toString().length() >= 6) {
            tv_btn_login.setEnabled(true);
        } else {
            tv_btn_login.setEnabled(false);
        }
    }

    void getMsgCode() {
        String phone = editTextPhone.getText().toString();
        String code = CodeUtils.getInstance().getCode();
//        if (!StringUtils.isNullOrEmpty(phone) || !StringUtils.isMobileNO(phone)) {
//            ToatUtils.toastSuccess(getContext(), "输入正确的手机号码");
//            return;
//        }
//        if (!code.equals(editTextCode.getText().toString())) {
//            ToatUtils.toastSuccess(getContext(), "请输入正确的随机码！");
//            return;
//        }

        timeDown();
        presenter.getMsgCode(getMCJson(phone, code));
    }

    String getMCJson(String phone, String code) {
        MsgCodePModel msgCodePModel = new MsgCodePModel();
        msgCodePModel.mobile = phone;
        msgCodePModel.authcode = code;
        String time = System.currentTimeMillis() / 1000 + "";
        msgCodePModel.time = time;
        msgCodePModel.md5authcode = DescriptUtils.SHA1(time + msgCodePModel.authcode + msgCodePModel.authcode + ConstantUtils.SHA1);
        return new Gson().toJson(msgCodePModel);
    }

    void timeDown() {
        textViewGetMsgCode.setEnabled(false);
        if (timeDown == null)
            timeDown = new TimeDown(60, new TimeDown.TimeDownListener() {
                @Override
                public void onPrepareCountDown() {

                }

                @Override
                public void onRefreshTime(int second) {
                    textViewGetMsgCode.setText(second + "秒");
                }

                @Override
                public void onFinishCountDown() {
                    textViewGetMsgCode.setText("重新获取");
                    //textViewGetMsgCode.setEnabled(true);
                    timeDown = null;
                    changeGetMsgCode();
                }
            });
        timeDown.start();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (timeDown != null) {
            timeDown.cancel();
        }
    }

    @Override
    public void setPassSuccess(SetPassModel model) {
        if (model != null) {
            if (IsSuccessUtils.isSuccess(model.result)) {
                SharePreferencesUtils.setLoginKey(model.loginkey);
                SharePreferencesUtils.setUid(model.uid);
                AppManager.getAppManager().notFinishActivity(MainActivity.class);
                LoginEvent loginEvent = LoginEvent.getLoginEvent();
                loginEvent.login = true;
                RxBus.getInstance().post(loginEvent);
            }else{
                ToatUtils.toastSuccess(getContext(),model.msg);
            }
        }
    }

    @Override
    public void sendSuccess(ResultModel model) {
        if (model != null) {
            if (IsSuccessUtils.isSuccess(model.result)) {
                ToatUtils.toastSuccess(getContext(), "验证码已经发送！");
            }else{
                ToatUtils.toastSuccess(getContext(),model.msg);
            }
        }
    }
}
